﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using DevExpress.XtraBars.Ribbon;
namespace NotebookS2
{
    public partial class M_EMPLOYEE : RibbonForm
    {
        public M_EMPLOYEE()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }


        void BindDepartment()
        {
            DepartmentManager oManager = new DepartmentManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable _table = oManager.GetDepartment();
            oManager.Close();
            cboDepartment.Properties.DataSource = _table;
            cboDepartment.Properties.ValueMember = "DeptName";
            cboDepartment.Properties.DisplayMember = "DeptName";
            cboDepartment.Refresh();

        }

        void BindEmployees(string _Department)
        {
            EmployeeManager oManager = new EmployeeManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable _table = oManager.GetEmployeesByDepartment(_Department);

            oManager.Close();

            cboSuperior.Properties.DataSource = _table;
            cboSuperior.Properties.ValueMember = "EmpId";
            cboSuperior.Properties.DisplayMember = "FullName";
            cboSuperior.Refresh();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void M_EMPLOYEE_Load(object sender, EventArgs e)
        {
            BindDepartment();
            BindEmployees(cboDepartment.Text);
        }

        void Clear()
        {
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtMiddleName.Text = "";
            txtExtension.Text = "";

            cboDepartment.Text = "";
            txtPosition.Text = "";
            cboSuperior.Text = "";

            txtStatus.Text = "Active";
            txtEmpID.Text = "";

        }

        public void ShowSearch()
        {
            FND_EMPLOYEE oSearch = new FND_EMPLOYEE();
            EmployeeManager oManger = new EmployeeManager();
            oSearch.ConnectionString = this.ConnectionString;
            oSearch.ShowDialog();

            string sEmpID = oSearch.EmployeeID;

            if (sEmpID != null)
            {
                txtEmpID.Text = sEmpID;

                oManger.ConnectionString = this.ConnectionString;
                oManger.Open();
                DataTable _Table = oManger.GetEmployeeInfo(sEmpID);
                oManger.Close();

                DataRow oRow = _Table.Rows[0];

                txtLastName.Text = oRow["LastName"].ToString();
                txtFirstName.Text = oRow["FirstName"].ToString();
                txtMiddleName.Text = oRow["MiddleName"].ToString();
                txtExtension.Text = oRow["ExtensionName"].ToString();
                txtPosition.Text = oRow["EmpPosition"].ToString();
                cboDepartment.Text = oRow["EmpDepartment"].ToString();
                cboDepartment.ClosePopup();
                cboSuperior.Text = oRow["EmpHead"].ToString();
                cboSuperior.ClosePopup();
                txtStatus.Text = oRow["EmpStatus"].ToString();

                btnAdd.Text = "Update";
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EmployeeManager oMgr = new EmployeeManager();
            EmployeeUnit oUnit = new EmployeeUnit();

            if (btnAdd.Text == "Add")
            {
                oMgr.ConnectionString = this.ConnectionString;
                oMgr.Open();

                oUnit.FirstName = txtFirstName.Text;
                oUnit.LastName = txtLastName.Text;
                oUnit.MiddleName = txtMiddleName.Text;
                oUnit.Extension = txtExtension.Text;

                oUnit.Department = cboDepartment.Text;
                oUnit.Position = txtPosition.Text;
                oUnit.Superior = cboSuperior.Text;
                oUnit.Status = txtStatus.Text;


                oMgr.ConnectionString = this.ConnectionString;

                oMgr.AddEmployee(oUnit);
                oMgr.Close();

                MessageBox.Show("New Employee information successfully added!!!", "Employee", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clear();

            }
            else
            {
                oMgr.ConnectionString = this.ConnectionString;
                oMgr.Open();
                oUnit.EmployeeID = txtEmpID.Text;
                oUnit.FirstName = txtFirstName.Text;
                oUnit.LastName = txtLastName.Text;
                oUnit.MiddleName = txtMiddleName.Text;
                oUnit.Extension = txtExtension.Text;

                oUnit.Department = cboDepartment.Text;
                oUnit.Position = txtPosition.Text;
                oUnit.Superior = cboSuperior.Text;
                oUnit.Status = txtStatus.Text;


                oMgr.ConnectionString = this.ConnectionString;

                oMgr.UpdateEmployee(oUnit);
                oMgr.Close();
                MessageBox.Show("Employee information successfully updated!!!", "Employee", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtBPCode_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }

        private void txtEmpID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                FND_EMPLOYEE oSearch = new FND_EMPLOYEE();
                EmployeeManager oManger = new EmployeeManager();
                oSearch.ConnectionString = this.ConnectionString;
                oSearch.ShowDialog();

                string sEmpID = oSearch.EmployeeID;

                if (sEmpID != null)
                {
                    txtEmpID.Text = sEmpID;

                    oManger.ConnectionString = this.ConnectionString;
                    oManger.Open();
                    DataTable _Table = oManger.GetEmployeeInfo(sEmpID);
                    oManger.Close();

                    DataRow oRow = _Table.Rows[0];

                    txtLastName.Text = oRow["LastName"].ToString();
                    txtFirstName.Text = oRow["FirstName"].ToString();
                    txtMiddleName.Text = oRow["MiddleName"].ToString();
                    txtExtension.Text = oRow["ExtensionName"].ToString();
                    txtPosition.Text = oRow["EmpPosition"].ToString();
                    cboDepartment.Text = oRow["EmpDepartment"].ToString();
                    cboDepartment.ClosePopup();
                    cboSuperior.Text = oRow["EmpHead"].ToString();
                    cboSuperior.ClosePopup();
                    txtStatus.Text = oRow["EmpStatus"].ToString();

                    btnAdd.Text = "Update";
                }
            }
            else
            {
                txtEmpID.Text = "";
                txtLastName.Text = "";
                txtFirstName.Text = "";
                txtMiddleName.Text = "";
                txtExtension.Text = "";
                txtPosition.Text = "";
                cboDepartment.EditValue = null;
                cboSuperior.EditValue = null;
                
                txtStatus.Text = "Active";

                btnAdd.Text = "Add";
            }
        }

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (txtEmpID.Text != "")
            {
                if (MessageBox.Show("Are you sure you to change the status of this Employee? Changing the status is irreversible.", "Status", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    EmployeeManager oManager = new EmployeeManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    oManager.ChangeStatusToResigned(txtEmpID.Text);
                    txtStatus.Text = "Resigned";

                    oManager.Close();
                }
            }
        }
    }
}
